using System;
using System.IO;
using System.Windows.Forms;

namespace SPDevExplorer
{
    internal class DragHandler
    {
        private readonly TreeView _ctlExplorerTree;
        private TreeNode _lastDragOverNode = null;
        private ServiceManager _serviceManager;

        public DragHandler(TreeView treeViewToManage, ServiceManager svcManager)
        {
            _serviceManager = svcManager;
            _ctlExplorerTree = treeViewToManage;
            //_ctlExplorerTree.DragDrop += new DragEventHandler(_ctlExplorerTree_DragDrop);
            _ctlExplorerTree.DragEnter += new DragEventHandler(_ctlExplorerTree_DragEnter);
            _ctlExplorerTree.DragOver += new DragEventHandler(_ctlExplorerTree_DragOver);
            _ctlExplorerTree.GiveFeedback += new GiveFeedbackEventHandler(_ctlExplorerTree_GiveFeedback);
            _ctlExplorerTree.ItemDrag += new ItemDragEventHandler(_ctlExplorerTree_ItemDrag);
        }

        private void _ctlExplorerTree_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (e.Item is TreeNode)
            {
                TreeNode tn = (TreeNode) e.Item;
                FileProperties p = new FileProperties(tn);
                if (p.Xml.Name == "File")
                {
                    FolderProperties fp = new FolderProperties(tn.Parent);
                    string sPath = SiteExplorer.EnsurePath(tn.Parent);
                    if (!fp.ReadOnly && File.Exists(sPath + "\\" + tn.Text))
                        _ctlExplorerTree.DoDragDrop(tn, DragDropEffects.Copy | DragDropEffects.Move);
                }
            }
        }

        private void _ctlExplorerTree_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            e.UseDefaultCursors = true;
        }

        private void _ctlExplorerTree_DragOver(object sender, DragEventArgs e)
        {
            try
            {
                e.Effect = DragDropEffects.None;

                System.Drawing.Point pt = _ctlExplorerTree.PointToClient(new System.Drawing.Point(e.X, e.Y));
                TreeNode oToNode = _ctlExplorerTree.GetNodeAt(pt);
                FolderProperties fp = new FolderProperties(oToNode);
                if (!fp.ReadOnly && fp.Xml.Name == "Folder")
                {
                    if ((e.KeyState & 8) == 8)
                        e.Effect = DragDropEffects.Move;
                    else
                        e.Effect = DragDropEffects.Copy;
                }
            }
            catch (Exception ex)
            {
                ((SiteExplorer) _ctlExplorerTree.Parent).LogEvent(ex, false);
            }
        }

        private void _ctlExplorerTree_DragEnter(object sender, DragEventArgs e)
        {
        }

        private void _ctlExplorerTree_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                System.Drawing.Point pt = _ctlExplorerTree.PointToClient(new System.Drawing.Point(e.X, e.Y));
                TreeNode oToNode = _ctlExplorerTree.GetNodeAt(pt);

                TreeNode oFromNode = (TreeNode) e.Data.GetData(typeof (TreeNode));
                string[] arFiles = (string[]) e.Data.GetData(DataFormats.FileDrop);

                if (oFromNode != null)
                    HandleTreeNode(oFromNode, oToNode);
                else if (arFiles != null)
                    HandleFileDrop(arFiles, oToNode);
            }
            catch (Exception ex)
            {
                ((SiteExplorer) _ctlExplorerTree.Parent).LogEvent(ex, false);
            }
        }

        private void HandleFileDrop(string[] arFiles, TreeNode oToNode)
        {
            MessageBox.Show(arFiles.ToString());
        }

        private void HandleTreeNode(TreeNode oFromNode, TreeNode oToNode)
        {
            string sFromPath = SiteExplorer.EnsurePath(oFromNode.Parent);
            string sToPath = SiteExplorer.EnsurePath(oToNode);

            FolderProperties fp = new FolderProperties(oToNode);
            oToNode.TreeView.SelectedNode = oToNode;

            SiteExplorer.CreateFile(oFromNode, NewFilename(oFromNode, oToNode), fp);

            MessageBox.Show(oFromNode.Text + " -> " + oToNode.Text);
        }

        private static string NewFilename(TreeNode oFromNode, TreeNode oToNode)
        {
            bool bExists = false;

            foreach (TreeNode oChild in oToNode.Nodes)
            {
                if (oChild.Text == oFromNode.Text)
                {
                    bExists = true;
                    break;
                }
            }

            string sFileName = oFromNode.Text;
            if (bExists) sFileName = "Copy of " + sFileName;
            return sFileName;
        }
    }
}